package com.livepan.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.livepan.server.dao.RoleResourceDOMapper;
import com.livepan.server.dataobject.RoleResourceDO;
import com.livepan.server.dataobject.RoleResourceDOExample;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;
import com.livepan.server.service.model.RoleResourceModel;
import com.livepan.server.service.model.PageModel;
import com.livepan.server.utils.CopyUtil;
import com.livepan.server.utils.UUIDUtil;
import com.livepan.server.utils.ValidatorUtil;
import com.livepan.server.validator.ValidationResult;
import com.livepan.server.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.CharsetMapping;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleResourceService {
    @Autowired
    private RoleResourceDOMapper roleResourceDOMapper;

    @Autowired
    private ValidatorImpl validator;

    public List<RoleResourceModel> list(String roleId) {
        RoleResourceDOExample roleResourceDOExample = new RoleResourceDOExample();
        RoleResourceDOExample.Criteria criteria = roleResourceDOExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        roleResourceDOExample.setOrderByClause("id asc");
        List<RoleResourceDO> roleResourceDOList = roleResourceDOMapper.selectByExample(roleResourceDOExample);

        List<RoleResourceModel> roleResourceModelList = roleResourceDOList.stream().map(
                this::convertRoleResourceModelFromRoleResourceDO
        ).collect(Collectors.toList());
        return roleResourceModelList;
    }


    public void save(RoleResourceModel roleResourceModel) throws BusinessException {
        ValidationResult validateResult = validator.validate(roleResourceModel);
        if (validateResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validateResult.getErrMsg());
        }
        RoleResourceDO roleResourceDO = CopyUtil.copy(roleResourceModel, RoleResourceDO.class);
        if (roleResourceDOMapper.selectByPrimaryKey(roleResourceDO.getId()) == null) {
            this.insert(roleResourceDO);
        } else {
            this.update(roleResourceDO);
        }
    }

    public void clear(String roleId) {
        RoleResourceDOExample example = new RoleResourceDOExample();
        RoleResourceDOExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        roleResourceDOMapper.deleteByExample(example);
    }

    private void insert(RoleResourceDO roleResourceDO) {
        roleResourceDO.setId(UUIDUtil.getShortUUID());
        roleResourceDOMapper.insert(roleResourceDO);
    }

    private void update(RoleResourceDO roleResourceDO) {
        roleResourceDOMapper.updateByPrimaryKey(roleResourceDO);
    }

    public void delete(String id) {
        roleResourceDOMapper.deleteByPrimaryKey(id);
    }


    private RoleResourceModel convertRoleResourceModelFromRoleResourceDO(RoleResourceDO roleResourceDO) {
        if (roleResourceDO == null) {
            return null;
        }
        RoleResourceModel roleResourceModel = new RoleResourceModel();
        BeanUtils.copyProperties(roleResourceDO, roleResourceModel);
        return roleResourceModel;
    }
}
